const gettype = Object.prototype.toString;
const $common = {
    // 类型校验
    isObj: function (o) {
        return gettype.call(o) == "[object Object]";
    },
    isArray: function (o) {
        return gettype.call(o) == "[object Array]";
    },
    isNULL: function (o) {
        return gettype.call(o) == "[object Null]";
    },
    isUndefined: function (o) {
        return gettype.call(o) == "[object Undefined";
    },
    isBoolean: function (o) {
        return gettype.call(o) == "[object Boolean]";
    },
    isString: function (o) {
        return gettype.call(o) == "[object String]";
    },
    trim(val) {//去除首尾空格
        return (val + '').replace(/(^\s*)|(\s*$)/g, "");
    },
    delcommafy(num) {/*去除千分位*/
        if (!num) {
            return "";
        }
        num = (num + '').replace(/,/gi, '');
        return num;
    },
    formatMoney(val, digit) {//金额保留两位小数点并以逗号分割
        var digit = digit || 2;//digit保留位数，默认为2
        if (val === undefined || val === null || isNaN(val)) {
            return '0.00';
        } else {
            let num = parseFloat(Number(val)).toFixed(digit);
            let arr = num.split(".");
            let left = arr[0].split("").reverse();
            let str = "";
            for (let i = 0; i < left.length; i++) {
                str += left[i] + ((i + 1) % 3 == 0 && (i + 1) != left.length ? "," : "");
            }
            return str.split("").reverse().join("") + '.' + arr[1];
        }
    },
    keepFloat(val, digit) {//保留一位小数
        var digit = digit || 1;//digit保留位数，默认为1
        return parseFloat(Number(val)).toFixed(digit);
    },
    formatDate(date) {//格式化日期 xxxx-xx-xx;
        if (date === "" || date == undefined) {
            return "";
        } else {
            let today = new Date(date);
            let year = today.getFullYear();
            let month = ('0' + (today.getMonth() + 1)).slice(-2);
            let day = ('0' + today.getDate()).slice(-2);
            return `${year}-${month}-${day}`;
        }
    },
    celiFix(data, digit) {//向上取整，默认保留1位
        var digit = digit || 1;//digit保留位数，默认为1
        var num = Math.pow(10, digit);
        data = (+data) * num;
        let ceilData = Math.ceil(data);
        let fixData = (ceilData / num).toFixed(digit);
        return fixData;
    },
    floorFix(data, digit) {//向下取整，默认保留1位
        var digit = digit || 1;//digit保留位数，默认为1
        var num = Math.pow(10, digit);
        data = (+data) * num;
        let ceilData = Math.floor(data);
        let fixData = (ceilData / num).toFixed(digit);
        return fixData;
    },
    repeatRemove: function (arr1, arr2, state) {
        //默认传一个数组，返回去重后的数据
        //state=0合并两个数组，去重返回新数组
        //state=1传入两个数组arr1、arr2，返回arr2中不带有arr1存在的数据
        //state=2传入两个数组arr1、arr2，返回重复的数据并去重
        var arr1 = arr1 || [];
        var arr2 = arr2 || [];
        var state = state || 0;
        var result = clearRepeat(arr1, arr2, state);;
        return result;


        function clearRepeat(arr1, arr2, state) {
            if (state == 0) {//合并两个数组，去重返回新数组
                var arr = [...arr1, ...arr2].map((item) => JSON.stringify(item));
                var result = [...new Set(arr)].map((item) => JSON.parse(item));
                return result;
            }
            arr1 = arr1.map((item) => JSON.stringify(item));
            arr2 = arr2.map((item) => JSON.stringify(item));
            var repeatList = [];
            for (let i = 0; i < arr1.length; i++) {
                for (let j = 0; j < arr2.length; j++) {
                    if (arr2[j] == arr1[i] && state == 2) {
                        repeatList.push(arr2[j])
                    } else if (arr2[j] == arr1[i]) {
                        arr2.splice(j, 1)
                        j--
                    }
                }
            }
            if (state == 2) {
                return [...new Set(repeatList)].map((item) => JSON.parse(item));
            }
            return [...new Set(arr2)].map((item) => JSON.parse(item));
        }
    },
    download: function (url, paramObj) {
        var doc = document;
        //使用一个隐藏的form表单执行提交，没有则创建
        var downloadForm = doc.getElementById('_downloadForm');
        if (!downloadForm) {
            downloadForm = doc.createElement('form');
            downloadForm.id = '_downloadForm';
            doc.body.appendChild(downloadForm);//必须加入body中，否则可能不下载
        }
        downloadForm.method = 'post';
        downloadForm.enctype = 'application/json';//请求头
        downloadForm.acceptCharset = 'UTF-8';//请求头
        downloadForm.action = url;
        downloadForm.style.display = 'none';//隐藏
        downloadForm.target = '_blank';
        //移除之前表单内的所有域
        while (downloadForm.hasChildNodes()) {
            downloadForm.removeChild(downloadForm.firstChild);
        }
        //根据传入的参数，创建表单隐藏域
        if (paramObj != null) {
            var hidden;
            for (var key in paramObj) {
                if (paramObj[key] == null) {
                    continue;
                }
                hidden = doc.createElement('input');
                hidden.type = 'hidden';
                hidden.name = key;
                hidden.value = paramObj[key];
                downloadForm.appendChild(hidden);
            }
        }
        downloadForm.submit();//提交，执行下载
    },
    /*身份证校验*/
    IdentityCodeValid(code) {
        var city = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外 " };
        var tip = "";
        var pass = true;
        if (!code || !/^\d{6}(18|19|20){1}\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
            tip = "身份证号格式错误";
            pass = false;
        } else if (!city[code.substr(0, 2)]) {
            tip = "地址编码错误";
            pass = false;
        } else {
            //18位身份证需要验证最后一位校验位
            if (code.length == 18) {
                code = code.split('');
                //∑(ai×Wi)(mod 11)
                //加权因子
                var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
                //校验位
                var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
                var sum = 0;
                var ai = 0;
                var wi = 0;
                for (var i = 0; i < 17; i++) {
                    ai = code[i];
                    wi = factor[i];
                    sum += ai * wi;
                }
                var last = parity[sum % 11];
                if (parity[sum % 11] != code[17]) {
                    tip = "校验位错误";
                    pass = false;
                }
            }
        }
        if (!pass) alert(tip);
        return pass;
    },
}